package kr.co.redcircle.loader.utility;

public class ByteUtil {
	
	public static byte[] getSize(byte[] b, int dataIndex) {
		return getSize(b, 3, dataIndex);
	}
	
	public static byte[] getSize(byte[] b, int length, int dataIndex) {
		byte[] tmpByteArray = new byte[length];
		System.arraycopy(b, dataIndex, tmpByteArray, 0, length);
		return tmpByteArray;
	}

	public static Integer BytesIndexOf(byte[] Source, byte[] Search, int fromIndex) {
		boolean Find = false;
		int i;
		for (i = fromIndex; i < Source.length - Search.length; i++) {
			if (Source[i] == Search[0]) {
				Find = true;
				for (int j = 0; j < Search.length; j++) {
					if (Source[i + j] != Search[j]) {
						Find = false;
					}
				}
			}
			if (Find) {
				break;
			}
		}
		if (!Find) {
			return new Integer(-1);
		}
		return new Integer(i);
	}

	public static int byteArrayToInt(byte[] b) 
	{
	    int value = 0;
	    for (int i = 0; i < b.length; i++) {
	        int shift = (b.length - 1 - i) * 8;
	        value += (b[i] & 0x000000FF) << shift;
	    }
	    return value;
	}

	public static final byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
	}
}
